package org.androidpn.server.entity;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.androidpn.server.util.XMLUtils;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

/**
 * entity 游戏房间消息
 * 
 * @author 蔡琪
 * 
 */
@Entity
@Table(name = "tk_room_message")
public class RoomMessage extends BaseEntity {

	private static final long serialVersionUID = 7725100928402510829L;

	/**
	 * 游戏房间信息类型
	 * 
	 * @author 蔡琪
	 * 
	 */
	public enum RoomMessageType {
		/** 文本 */
		text,
		/** MP3 */
		mp3
	}

	/** 游戏房间消息类型 */
	@Column(name = "type")
	private RoomMessageType type;

	/** 内容 */
	@Column(name = "content")
	private String content;

	/** 发送消息的角色 */
	@ManyToOne(cascade = CascadeType.REFRESH, fetch = FetchType.EAGER)
	private Role role;

	/** 游戏房间信息 */
	@ManyToOne(cascade = CascadeType.REFRESH, fetch = FetchType.EAGER)
	private Room room;

	public RoomMessageType getType() {
		return type;
	}

	public void setType(RoomMessageType type) {
		this.type = type;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public Role getRole() {
		return role;
	}

	public void setRole(Role role) {
		this.role = role;
	}

	public Room getRoom() {
		return room;
	}

	public void setRoom(Room room) {
		this.room = room;
	}

	/**
	 * 从xml字符串中解析出Room
	 * 
	 * @param xml
	 *            xml字符串
	 * @return Room对象
	 */
	@Transient
	public static RoomMessage fromXML(String xml) {
		RoomMessage roomMessage = BaseEntity.fromXML(xml, RoomMessage.class);
		Document doc;
		try {
			doc = DocumentHelper.parseText(xml);
			Element element = doc.getRootElement();
			// 组装Room信息
			String type = element.elementText("type");
			String content = element.elementText("content");
			/** 发送消息的角色信息 */
			Role role = null;
			Element roleElement = element.element("role");
			if (roleElement != null) {
				role = Role.fromXML(roleElement.asXML());
			}
			Room room = null;
			Element roomElement = element.element("room");
			if (roomElement != null) {
				room = Room.fromXML(roomElement.asXML());
			}
			roomMessage.setType(XMLUtils.getRoomMessageTypeFromStr(type));
			roomMessage.setContent(content);
			roomMessage.setRole(role);
			roomMessage.setRoom(room);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return roomMessage;
	}

	/**
	 * 将当前对象转化为xml字符串
	 * 
	 * @return
	 */
	@Transient
	public String toXML() {
		StringBuffer buffer = new StringBuffer();
		buffer.append("<roomMessage>");
		buffer.append(super.toXML());
		buffer.append(XMLUtils.createSingleNode("type", this.type));
		buffer.append(XMLUtils.createSingleNode("content", this.content));
		if (this.role != null) {
			buffer.append(role.toXML());
		}
		if (this.room != null) {
			buffer.append(room.toXML());
		}
		buffer.append("</roomMessage>");
		return buffer.toString();
	}
}
